﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_CalendarControl : System.Web.UI.UserControl
{
    public LocalFunctions FUNC;
    DataTable DTEvents;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (FUNC.LangID == 2)
        {
            Calendar1.Attributes.Add("dir", "rtl");
            Calendar1.NextPrevStyle.Reset();
            Calendar1.PrevMonthText="<img src='images/arrow_r.png' border='0' />";
            Calendar1.NextMonthText = "<img src='images/arrow_l.png' border='0' />";
        }
        if (!IsPostBack)
        {
            if (Session["CalendarDate"] != null)
            {
                Calendar1.VisibleDate =(DateTime)Session["CalendarDate"];
            }
            else
            {
                Calendar1.VisibleDate = DateTime.Now;
            }
            //Dal.arParams.Clear();
            //Dal.arParams.Add(FUNC.LangID);
            //Dal.arParams.Add(DateTime.Now.Month);
            //Dal.arParams.Add(DateTime.Now.Year);
            //DTEvents = Dal.ExecuteDBsp("Admin_GetEventsByMonth", Dal.arParams);
            //Calendar1.VisibleDate = DateTime.Now;
        }
        //else
        //{
            Dal.arParams.Clear();
            Dal.arParams.Add(FUNC.LangID);
            Dal.arParams.Add(Calendar1.VisibleDate.Month);
            Dal.arParams.Add(Calendar1.VisibleDate.Year);
            DTEvents = Dal.ExecuteDBsp("Admin_GetEventsByMonth", Dal.arParams);
        //}

    }

    protected void Calendar_OnDayRender(Object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
        bool isEvent = false;
        if (DTEvents != null)
        {
            if (DTEvents.Rows.Count > 0)
            {
                foreach (DataRow DR in DTEvents.Rows)
                {
                    DateTime EventDate = DateTime.Parse(DR["StartDate"].ToString());
                    if (EventDate.Date == e.Day.Date)
                    {
                        if (!e.Day.IsOtherMonth)
                        {
                            e.Cell.CssClass = "daysitemEvent";
                        }
                        if (e.Day.IsToday)
                        {
                            e.Cell.CssClass = "todayitemEvent";
                        }
                    }
                }
            }
        }
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        string sLink=string.Format("Events.aspx?type={0}&date={1}",(int)CalendarPageType.ByMonth,((Calendar)sender).SelectedDate.ToBinary());
        Response.Redirect(sLink);
    }

    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        Calendar1.VisibleDate = e.NewDate;
        Session["CalendarDate"] = Calendar1.VisibleDate;
        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID);
        Dal.arParams.Add(Calendar1.VisibleDate.Month);
        Dal.arParams.Add(Calendar1.VisibleDate.Year);
        DTEvents = Dal.ExecuteDBsp("Admin_GetEventsByMonth", Dal.arParams);
    }
}
